package com.bigger.service.impl;

import com.bigger.constant.ESConstant;
import com.bigger.entity.Business;
import com.bigger.entity.BusinessMP;
import com.bigger.mapper.BusinessMapper;
import com.bigger.service.BusinessService;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private RestHighLevelClient client;//es数据库操作客服端
    @Autowired
    private BusinessMapper businessMapper;
    private Gson gson=new Gson();
    @Override
    public List<BusinessMP> get(String key) {
        return ESGet(key);
    }

    @Override
    public BusinessMP getById(Long id) {
        BusinessMP businessMP = ESGetById(id);
        return businessMP;
    }

    @Override
    public void insertById(Long id) {
        Business business = businessMapper.selectById(id);
        BusinessMP businessMP = new BusinessMP(business);
        IndexRequest request=new IndexRequest(ESConstant.INDICES).id(businessMP.getId().toString());
        request.source(gson.toJson(businessMP), XContentType.JSON);
        try {
            client.index(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(Long id) {
        DeleteRequest request=new DeleteRequest(ESConstant.INDICES,id.toString());
        try {
            client.delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private BusinessMP ESGetById(Long id){
        try {
            SearchRequest request=new SearchRequest(ESConstant.INDICES);
            request.source().query(QueryBuilders.termQuery("id",id));
            SearchResponse search = client.search(request, RequestOptions.DEFAULT);
            List<BusinessMP> businessMPS = ESParseResponse(search);
            return businessMPS.get(0);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * es数据库获取信息
     * @return
     */
    private List<BusinessMP> ESGet(String key){
        SearchRequest request=new SearchRequest(ESConstant.INDICES);
        BoolQueryBuilder bool=new BoolQueryBuilder();
        bool.filter(QueryBuilders.termQuery("status", Business.UP_BUSINESS));//过滤出上架的商品
        if (!StringUtils.isEmpty(key)){
            bool.must(QueryBuilders.matchQuery("name",key));
        }
        request.source().query(bool);
        try {
            SearchResponse search = client.search(request, RequestOptions.DEFAULT);
            List<BusinessMP> businessMPS = ESParseResponse(search);
            return businessMPS;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private List<BusinessMP> ESParseResponse(SearchResponse response){
        SearchHit[] hits = response.getHits().getHits();
        List<BusinessMP> businessMPS=new ArrayList<>();
        for (SearchHit hit : hits) {
            String json = hit.getSourceAsString();//获取json字符串对象
            BusinessMP businessMP = gson.fromJson(json, BusinessMP.class);
            businessMPS.add(businessMP);
        }
        return businessMPS;
    }
}
